package pl.edu.uj.tcs.blef.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public final class ServerListener extends Thread {
	private ServerSocket socket;
	private static int SERVER_CONNECTIONS;
	
	public void decrementConnections(){
		if(SERVER_CONNECTIONS > 0) SERVER_CONNECTIONS--;
	}
	
	@Override
	public void run(){
		while(true){
			while(SERVER_CONNECTIONS <= GlobalSettings._MAX_SERVER_CONNECTIONS_){
				try {
					Socket s = socket.accept();
					SERVER_CONNECTIONS++;
					System.out.println("New incoming client connection...");
					new ServerConnectionHandler(this, s);
				} catch (IOException e) { e.printStackTrace(); }
			}
		}
	}
	
	public ServerListener(ServerSocket socket){
		this.socket = socket;
		start();
	}
}
